import * as XLSX from 'xlsx';
import {reactive} from "vue";
import {arrayChangeObject} from "@/utils/index.js";

export function importExcel(data) {
  let result = reactive([]);
  const file = data.file;
  const reader = new FileReader();
  reader.onload = (e) => {
    const data = new Uint8Array(e.target.result);
    const workbook = XLSX.read(data, {type: "array"});
    const sheetName = workbook.SheetNames[1];
    const sheet = workbook.Sheets[sheetName];
    // excel原始数据
    let excelData = XLSX.utils.sheet_to_json(sheet, {header: 1});
    // 转为数组对象格式
    result = arrayChangeObject(excelData);
  };
  reader.readAsArrayBuffer(file);
  return {result};
}
